package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.bean.CountChannelBean;
import com.topnav.mc.admin.entity.DeviceChannel;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceChannelBean;
import com.topnav.mc.admin.entity.param.DeviceChannelParam;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface DeviceChannelMapper extends McMapper<DeviceChannel, DeviceChannelBean, DeviceChannelParam> {
    String commSql = " SELECT c.*," +
            " d.code AS deviceCode, d.name AS deviceName, u.name AS userName, u.code AS userCode, " +
            " d.stream_encryption AS streamEncryption, " +
            " o.id AS orgId,o.code AS orgCode, o.name AS orgName, o.path AS path  " +
            " FROM mc_device_channel c " +
            " LEFT JOIN mc_device d ON c.device_id = d.id " +
            " LEFT JOIN mc_user u ON d.user_id = u.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " LEFT JOIN mc_gb_server g ON d.gb_server_id = g.id " +
            " ${ew.customSqlSegment}";


    @Select(commSql)
    DeviceChannelBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<DeviceChannelBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<DeviceChannelBean> selectInfoPage(Page<DeviceChannelParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);


    /**
     * 查询所有通道
     * ew.customSqlSegment 可以通过条件，查在线的，分部门的 等等
     * return
     *    orgId,path,deviceId,channelCount,channelId
     */
    String sql1 = "SELECT o.id AS orgId,o.path AS path,c.id AS channelId," +
            " d.id AS deviceId,d.channel_count AS channelCount,d.type,d.cate " +
            " FROM mc_device_channel c " +
            " LEFT JOIN mc_device d ON c.device_id = d.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " ${ew.customSqlSegment}";
            //"WHERE c.`status` = 1 AND d.`online` = 1 AND  not ISNULL(d.org_id)"
    @Select(sql1)
    //List<Map<String,Object>> countChannel(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    List<CountChannelBean> countChannel(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    /**
     * 按单位用设备统计通道的数量
     *  return
     *     orgId,deviceId,total,orgCode,path
     */
/*    String countByOrgSql = " SELECT d.org_id AS orgId,c.device_id AS deviceId,count(*) AS total," +
            " o.code AS orgCode, o.path " +
            " FROM mc_device_channel c " +
            " LEFT JOIN mc_device d ON c.device_id = d.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " ${ew.customSqlSegment} " ;
            //" GROUP BY d.org_id,c.device_id ";
    @Select(countByOrgSql)
    List<Map<String,Object>> countByOrgAndDevice(@Param(Constants.WRAPPER) Wrapper queryWrapper);*/

    /**
     * 按设备统计通道数
     * @param queryWrapper
     * @return
     */
    @Select("SELECT COUNT(*) AS total FROM mc_device_channel ${ew.customSqlSegment} ")
    Map<String,Object> countByDevice(@Param(Constants.WRAPPER) Wrapper queryWrapper);


    /**
     * 按关键字查询通道列表
     */
    String sqlByKeyword = " SELECT c.id,c.device_id AS deviceId, c.channel_code AS channelCode, " +
            " c.name AS name,c.status, " +
            " d.code AS deviceCode, d.name AS deviceName, d.type, d.online AS online, d.channel_count AS channelCount, " +
            " o.path AS path, d.org_id AS orgId " +
            " FROM mc_device_channel c " +
            " LEFT JOIN mc_device d ON c.device_id = d.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " ${ew.customSqlSegment}";
    @Select(sqlByKeyword)
    List<DeviceChannelBean> findChannelInfo(@Param(Constants.WRAPPER) Wrapper queryWrapper);

}
